import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.regex.Pattern;

import util.Properties;


public class Generator {
	
	private Random227 random;
	private FileWriter fileWriter;

	public Generator(String filePath) throws IOException {
		this.random = new Random227();
		
		this.fileWriter = new FileWriter(new File(filePath));
	}
	
	public void generateCenarios(int participantes, int... cenarios) throws IOException{
		this.generateCenariosTecnica("SVN", participantes,cenarios);
		this.generateCenariosTecnica("GIT", participantes,cenarios);
		
		this.fileWriter.close();
	}
	
	private void generateCenariosTecnica(String tecnica, int participantes, int... cenarios) throws IOException{
		for(int cenario : cenarios){
			for(int passo = 2; passo <= 5; passo++){
				this.generateTemposDosParticipantesParaPasso(tecnica,cenario,passo,participantes);
			}
			
			this.fileWriter.write("\n");
		}
	}
	
	private void generateTemposDosParticipantesParaPasso(String tecnica, int cenario, int passo, int partipantes) throws IOException{
		this.fileWriter.write(tecnica + ".Cenario" + cenario + ".Passo" + passo + " = ");
		
		for(int i = 0; i < partipantes; i++){
			if(i != 0){
				this.fileWriter.write("|");
			}
			
			this.generateTempoPasso(tecnica, cenario, passo);
		}
		
		this.fileWriter.write("\n");
	}
	
	private void generateTempoPasso(String tecnica, int cenario, int passo) throws IOException{
		double[] realSamples = this.getRealSamples(tecnica, cenario,passo);
		
		StatCalc statCalc = new StatCalc();
		statCalc.enter(realSamples);
		
		double media = statCalc.getMean();
		double desvio = statCalc.getStandardDeviation();
		
		int randomicTime = -1;
		
		while(randomicTime < 0){
			randomicTime = (int) this.random.nextGaussian(media, desvio);
		}
		
		this.fileWriter.write(this.secondsToString(randomicTime));
	}

	private String secondsToString(int tempoSegundos) {
		int horas = tempoSegundos/3600;
		int minutos = (tempoSegundos - horas*3600)/60;
		int segundos = tempoSegundos - horas*3600 - minutos*60;
		
		DecimalFormat df = new DecimalFormat("00");
		
		return df.format(horas) + ":" + df.format(minutos) + ":" + df.format(segundos);
	}

	private double[] getRealSamples(String tecnica, int cenario, int passo) {
		String[] tempos = Properties.getProperty(tecnica + ".Cenario" + cenario + ".Passo" + passo).split(Pattern.quote("|"));
		double[] retorno = new double[tempos.length];
		int proximaPosicao = 0;
		
		for(String tempo : tempos){
			retorno[proximaPosicao++] = (double)this.stringToSeconds(tempo.trim());
		}
		
		return retorno;
	}

	private int stringToSeconds(String tempo) {
		String[] horaMinutoSegundo = tempo.split(":");
		
		return Integer.parseInt(horaMinutoSegundo[0])*60*60 + Integer.parseInt(horaMinutoSegundo[1])*60 + Integer.parseInt(horaMinutoSegundo[2]);
	}
	
	private void createBase(String path, int qtdParticipantes, int[] cenarios) throws FileNotFoundException, IOException {
		java.util.Properties properties = new java.util.Properties();
		properties.load(new FileInputStream(path));
		
		DecimalFormat df = new DecimalFormat("00");
		
		for(int i = 0; i < qtdParticipantes; i++){
			FileWriter fileWriterSVN = new FileWriter(new File("base/voluntario" + df.format(i+1) + "svn.txt"));
			FileWriter fileWriterGIT = new FileWriter(new File("base/voluntario" + df.format(i+1) + "git.txt"));
			
			for(int cenario : cenarios){
				fileWriterSVN.write("cen" + df.format(cenario));
				fileWriterGIT.write("cen" + df.format(cenario));

				for(int j = 2; j <= 5; j++){
					String linha = properties.getProperty("SVN" + ".Cenario" + cenario + ".Passo" + j);
					
					String[] tempos = linha.split(Pattern.quote("|"));		
					
					fileWriterSVN.write("," + this.stringToSeconds(tempos[i]));
					
					fileWriterGIT.write("," + this.stringToSeconds(properties.getProperty("GIT" + ".Cenario" + cenario + ".Passo" + j)
							.split(Pattern.quote("|"))[i]));
				}
				
				fileWriterSVN.write("\n");
				fileWriterGIT.write("\n");
			}
			fileWriterSVN.close();
			fileWriterGIT.close();
		}
	}

	public static void main(String[] args) {
		String path = "AmostrasRandomicas.txt";
		String pathAmostrasParaConverter = "properties/AmostrasSergioFinais.txt";
		int qtdParticipantes = 5;
		int[] cenarios = {0,1,2,3,4,5};
		
		try {
			Generator generator = new Generator(path);
			
	//		generator.generateCenarios(qtdParticipantes, cenarios);
			
			generator.createBase(pathAmostrasParaConverter, qtdParticipantes, cenarios);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
